/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    // var sort_id = $('#sort_id').val();
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        uid: uinfo.uid,
        sort_id: sort_id,
        start: pageStart, limit: pages.limit,
    };
    console.log(formData); //return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var uinfo = getAdminInfo();
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].in_day + '</td>';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_str + '</td>';
        h += '<td>' + d[i].delay_str + '</td>';
        h += '<td>' + d[i].staff_name + '</td>';
        // h += '<td>' + d[i].is_pub + '</td>';
        h += '<td>';
        h += d[i].is_pub;
        if (d[i].status_id == 4 && d[i].refuse_msg) {
            h += '<div style="max-width: 140px;">' + d[i].refuse_msg + '</div>';
        }
        h += '</td>';
        if (uinfo.role_id != 2) {
            h += '<td class="td-manage">';
            if (d[i].status_id < 3 || d[i].status_id == 4) {
                h += '<a href="javascript:;" class="ml-5" onclick="window.location.href=\'wh-edit.html?id=' + d[i].id + '\';">';
                h += '编辑';
                h += '<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += '&nbsp;&nbsp;';
            }
            if (d[i].status_id < 5) {
                h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
                h += ' 删除';
                h += '<i class="Hui-iconfont"></i>';
                h += '</a>';
            } else {
                // h += '-';
                h += '<a href="javascript:;" class="ml-5" onclick="window.location.href=\'wh-one.html?id=' + d[i].id + '\';">';
                h += '详情';
                h += '</a>';
                h += '&nbsp;&nbsp;';
            }
            h += '</td>';
        }
        h += '</tr>';
    }
    return h;
}


function toJump(){
    var sort_id = $('#sort_id').val();
    var p = '?sort_id=' + sort_id;
    window.location.href = 'workhour.html' + p;
}


function getOption(){
    var sh = '';
    var sortDiv = $('#sort_id'), sh = '';
    if (sort_id == 2) {
        sh += '<option value="1">按日期正序</option>';
        sh += '<option value="2" selected>按日期倒序</option>';
    } else {
        sh += '<option value="1" selected>按日期正序</option>';
        sh += '<option value="2">按日期倒序</option>';
    }
    sortDiv.html(sh);
}


//设置项目
function setByItem(){
    var uinfo = getAdminInfo();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var in_day = $('#in_day').val();
    var formData = {
        id: id, item_id: item_id, task_id: task_id, uid: uinfo.uid,
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
        in_day: in_day,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/setbyitem',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //window.location.reload();
            window.location.href = 'wh-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//设置默认项目
function itemDefault(){
    var uinfo = getAdminInfo();
    var item_id = $('#item_id').val();
    var formData = {
        id: id, item_id: item_id, uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/item-default/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            window.location.reload();
            //window.location.href = 'wh-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//选项
function getOne(){
    var uinfo = getAdminInfo();
    var formData = {
        id: id, depart_id: uinfo.depart_id, uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            console.log(res.data);
            oneHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//详情HTML
function oneHtml(e){
    var uinfo = getAdminInfo();
    console.log(uinfo);
    var d = e.data;
    var itemList = e.item;
    var taskList = e.task;
    var h, i, default_item_id = 0;
    //日期
    $('#in_day')[0].value = d.in_day;
    //项目
    h = '';
    if (d.item_id == 0 && d.item_default == 0) {
        h += '<option value="0" selected>-选择项目-</option>';
    } else {
        h += '<option value="0">-选择项目-</option>';
    }
    for (i = 0; i < itemList.length; i ++) {
        // if (d.item_id == itemList[i].id || 
        //     (d.item_default == itemList[i].id && d.item_default == d.item_id) 
        //     || d.item_default == itemList[i].id) {
        //     h += '<option value="' + itemList[i].id + '" selected>';
        // } else  {
        //     h += '<option value="' + itemList[i].id + '">';
        // }
        if (d.item_id > 0 && d.item_id == itemList[i].id) { //已选的
            h += '<option value="' + itemList[i].id + '" selected>';
        } else if (d.item_id == 0 && d.item_default == itemList[i].id) { //未选的
            h += '<option value="' + itemList[i].id + '" selected>';
        } else {
            h += '<option value="' + itemList[i].id + '">';
        }
        if (d.item_default == itemList[i].id) {
            h += '【默认项】';
            default_item_id = itemList[i].id;
        }
        h += itemList[i].title;
        h += '</option>';
    }
    $('#item_id').html(h);
    var defaultDiv = $('#isdefault');
    // if (d.item_id == d.item_default || default_item_id == d.item_default) {
    //     defaultDiv.html('<a href="javascript:;">当前项已是默认</a>');
    // } else {
    //     defaultDiv.html('<a href="javascript:;" style="color: #0088CC;" onclick="itemDefault();">当前项设置为默认</a>');
    // }
    if (d.item_id > 0 && d.item_id == d.item_default) { //已选的情况
        defaultDiv.html('<a href="javascript:;">当前项已是默认</a>');
    } else if (d.item_id == 0) { //未选有默认的情况
        if (d.item_default > 0 && d.item_default == default_item_id) { //有默认
            defaultDiv.html('<a href="javascript:;">当前项已是默认</a>');
        } else {
            // 
        }
    } else {
        defaultDiv.html('<a href="javascript:;" style="color: #0088CC;" onclick="itemDefault();">当前项设置为默认</a>');
    }
    //任务
    h = '';
    if (d.task_id == 0) {
        h += '<option value="0" selected>-选择任务-</option>';
    } else {
        h += '<option value="0">-选择任务-</option>';
    }
    for (i = 0; i < taskList.length; i ++) {
        if (d.task_id == taskList[i].id) {
            h += '<option value="' + taskList[i].id + '" selected>';
        } else {
            h += '<option value="' + taskList[i].id + '">';
        }
        h += taskList[i].title;
        h += '（' + taskList[i].depart_name + '|' + taskList[i].in_day + '）';
        h += '</option>';
    }
    $('#task_id').html(h);
    $('#delay_total')[0].value = d.delay;
    //工时项
    var dwh = d.whList, ii, child;
    h = '';
    for (i = 0; i < dwh.length; i ++) {
        child = dwh[i].child;
        if (child.length > 0) {
            h += '<style type="text/css">.formGroup { margin: 10px 3vw; border: 1px solid #eee; clear: both; } .formBox { margin: 20px; height: 200px; border: 1px solid #eee; clear: both; } .gsSub { padding: 0 10px; padding-top: 5px; color: #0088CC; float: right; } .wh-child { width: 85%; }</style>';
            h += '<div class="formGroup">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '工时项：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + dwh[i].config_title + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" onclick="child_add(' + dwh[i].id + ');">新增内容</a>';
            h += '<input type="hidden" id="config_whid' + dwh[i].id + '" value="' + dwh[i].config_whid + '"/>';
            h += '<input type="hidden" id="config_title' + dwh[i].id + '" value="' + dwh[i].config_title + '"/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '合计工时：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + dwh[i].delay + '" readonly/>';
            h += '</div>';
            h += '</div>';
        }
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div class="formBox">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '工作内容：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            //h += '<input type="text" class="input-text wh-child" value="' + child[ii].title + '" disabled/>';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].cnt_str + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" onclick="child_edit(' + child[ii].id + ');">再次编辑</a>';
            h += '<input type="hidden" id="pid' + child[ii].id + '" value="' + dwh[i].id + '"/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '开始时间：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].starthour + '" disabled/>';
            h += '<a href="javascript:;" class="gsSub" style="color: red;" onclick="itemDel(' + child[ii].id + ');">删除</a>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '结束时间：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].endhour + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '时长：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].delay_str + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '</div>';
        }
        h += '</div>';
    }
    $('#formEdit').html(h);
}


// 新增父级
function whNewAdd(title,url){
    var uinfo = getAdminInfo();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var in_day = $('#in_day').val();
    var formData = {
        id: id, item_id: item_id, task_id: task_id, uid: uinfo.uid,
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
        in_day: in_day,
    };
    // console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/setbyitem',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var wh_id = res.data;
            var item_id = $('#item_id').val();
            if (item_id == 0) {
                layerMsgErr('项目必须的'); return
            }
            url += '?id=' + 0;
            url += '&wh_id=' + wh_id;
            layer_show(title,url,null,380);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 获取任务列表
function getTaskList(){
    var uinfo = getAdminInfo();
    var formData = {
        in_day: $('#in_day').val(),
        depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/tasklist',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i = 0;
            h = '<option value="">-选择-</option>';
            for (; i< d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '（' + d[i].depart_name + '|' + d[i].in_day + '）';
                h += '</option>';
            }
            $('#task_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//新增子级
function child_add(v){
    var config_whid = $('#config_whid' + v).val();
    var config_title = $('#config_title' + v).val();
    //console.log(v, config_whid, config_title); return;
    var item_id = $('#item_id').val();
    if (item_id == 0) {
        layerMsgErr('项目必须的'); return
    }
    var title = '修改子工时';
    var url = 'wh-item.html';
    url += '?id=' + 0;
    url += '&wh_id=' + id;
    url += '&config_whid=' + config_whid;
    url += '&config_title=' + escape(config_title);
    layer_show(title,url,null,380);
}


//修改子级
function child_edit(v){
    var pid = $('#pid' + v).val();
    var config_whid = $('#config_whid' + pid).val();
    var config_title = $('#config_title' + pid).val();
    var item_id = $('#item_id').val();
    if (item_id == 0) {
        layerMsgErr('项目必须的'); return
    }
    var title = '修改子工时';
    var url = 'wh-item.html';
    url += '?id=' + v;
    url += '&wh_id=' + id;
    url += '&config_whid=' + config_whid;
    url += '&config_title=' + escape(config_title);
    layer_show(title,url,null,380);
}


//删除
function itemDel(v){
    var formData = {
        id: v,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//保存
function modify(){
    var in_day = $('#in_day').val();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    if (in_day == '') {
        msgPop({msg: '日期必须的'}); return
    }
    var formData = {
        id: id, in_day: in_day,
        item_id: item_id, task_id: task_id,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                //window.top.location.reload();
                window.location.href = '/admin/yw-wh/workhour.html';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//发布
function pub(){
    var formData = {
        id: id,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/pub',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                //window.top.location.reload();
                window.location.href = '/admin/yw-wh/workhour.html';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}